﻿using src.Domain;

namespace src.Helper
{
    public class WCBase
    {
        private string _appId { get; set; }

        private string _appSecret { get; set; }

        public WCBase(string appId, string appSecret)
        {
            this._appId = appId;
            this._appSecret = appSecret;
            bool isInit = CheckWeChatInfo();
            if (!isInit)
                throw new System.Exception("please input appid  and appsecret");
        }

        public WCBase(WeChatInfo weChatInfo)
        {
            this._appId = _appId;
            this._appSecret = _appSecret;
            bool isInit = CheckWeChatInfo();
            if (!isInit)
                throw new System.Exception("please input appid  and appsecret");
        }

        /// <summary>
        /// check _appid and _appsecret is null
        /// </summary>
        /// <returns></returns>
        private bool CheckWeChatInfo()
        {
            if (string.IsNullOrWhiteSpace(_appId) || string.IsNullOrWhiteSpace(_appSecret))
            {
                return false;
            }
            return true;
        }

        #region 基础Helper方法
        /// <summary>
        /// 获取accesstoken
        /// </summary>
        /// <returns></returns>
        public string GetAccessToken()
        {
            return BasicsHelper.GetAccessToken(this._appId, this._appSecret);
        }

        /// <summary>
        /// 获取微信服务器ip列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public string GetWeChatServerIps(string token = "")
        {
            if (string.IsNullOrWhiteSpace(token)) token = GetAccessToken();
            return BasicsHelper.GetWeChatServerIps(token);
        }

        /// <summary>
        /// 获取网络检测信息
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public string GetPingInfo(string token = "", NetCheckAction action = NetCheckAction.ALL, NetCheckOperator netCheckOperator = NetCheckOperator.DEFAULT)
        {
            if (string.IsNullOrWhiteSpace(token)) token = GetAccessToken();
            string netAction = action.ToString().ToLower();
            string netOperator = netCheckOperator.ToString();
            return BasicsHelper.GetPingInfo(token, netAction, netOperator);
        }
        #endregion

        #region 菜单Helper方法
        /// <summary>
        /// 创建菜单
        /// </summary>
        /// <param name="menuStr">菜单json</param>
        /// <param name="token">accesstoken</param>
        /// <returns></returns>
        public string CreateMenu(string menuStr, string token = "")
        {
            if (string.IsNullOrWhiteSpace(menuStr)) throw new System.Exception("需完善菜单信息");
            if (string.IsNullOrWhiteSpace(token)) token = GetAccessToken();
            string jsonReponse = MenuHelper.CreateMenu(menuStr, token);
            return jsonReponse;
        }

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="token">accesstoken</param>
        /// <returns></returns>
        public string GetMenu(string token = "")
        {
            if (string.IsNullOrWhiteSpace(token)) token = GetAccessToken();
            string jsonResponse = MenuHelper.GetMenu(token);
            return jsonResponse;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="token">accessToken</param>
        /// <returns></returns>
        public string DelMenu(string token = "")
        {
            if (string.IsNullOrWhiteSpace(token)) token = GetAccessToken();
            string jsonResponse = MenuHelper.DelMenu(token);
            return jsonResponse;
        }

        /// <summary>
        /// 创建个性化菜单
        /// </summary>
        /// <param name="menuStr">菜单json</param>
        /// <param name="accessToken">accesstoken</param>
        /// <returns></returns>
        public string CreateConditionalMenu(string menuStr, string token)
        {
            if (string.IsNullOrWhiteSpace(token)) token = GetAccessToken();
            string jsonResponse = MenuHelper.CreateConditionalMenu(menuStr, token);
            return jsonResponse;
        }

        /// <summary>
        /// 删除个性化菜单
        /// </summary>
        /// <param name="menuId">菜单id 可以通过自定义菜单查询接口获取</param>
        /// <param name="accessToken">accesstoken</param>
        public string DelConditionalMenu(string menuId, string token)
        {
            if (string.IsNullOrWhiteSpace(token)) token = GetAccessToken();
            string jsonResponse = MenuHelper.DelConditionalMenu(menuId, token);
            return jsonResponse;
        }

        /// <summary>
        /// 测试个性化菜单匹配结果
        /// </summary>
        /// <param name="userId">user_id可以是粉丝的OpenID，也可以是粉丝的微信号</param>
        /// <param name="accessToken">accesstoken</param>
        /// <returns></returns>
        public string TestConditionalMenu(string userId, string token)
        {
            if (string.IsNullOrWhiteSpace(token)) token = GetAccessToken();
            string jsonResponse = MenuHelper.TestConditionalMenu(userId, token);
            return jsonResponse;
        }
        #endregion

    }
}
